package com.wangzhenyu.common.loadmore

import android.graphics.Color
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import androidx.viewbinding.ViewBinding
import com.wangzhenyu.common.base.BasePresenter
import com.wangzhenyu.common.base.BaseVBPActivity
import com.wangzhenyu.common.databinding.FragmentBaseLoadMatchBinding
import com.wangzhenyu.common.view.CustomLoadMoreView

//T为当前数据类型
abstract class BaseLoadActivity<T, VB : ViewBinding, P : BasePresenter> : BaseVBPActivity<VB, P>() {

    protected lateinit var adapter: LoadMoreAdapter<T>

    protected lateinit var swipeLayout: SwipeRefreshLayout
    protected lateinit var recyclerView: RecyclerView
    protected lateinit var mLoadMoreLayout: FragmentBaseLoadMatchBinding

    //页面数据
    protected val pageInfo = PageInfo()

    //获取每页的数量
    protected open fun getPageSize() = 5

    override fun onStart() {
        super.onStart()
        swipeLayout.isRefreshing = true
        refresh()
    }

    override fun initView() {

        //绑定include layout
        mLoadMoreLayout = if (useBaseLayout()) {
            FragmentBaseLoadMatchBinding.bind(mRootBinding.root)
        } else {
            FragmentBaseLoadMatchBinding.bind(mBinding.root)
        }

        //在这里初始化Adapter
        adapter = createAdapter()
        //初始化SwipeRefreshLayout
        swipeLayout = mLoadMoreLayout.swipeLayout
        //初始化RecyclerView
        recyclerView = mLoadMoreLayout.recycler
        //初始化Adapter
        initAdapter()
        //初始化Recycler
        initRecycler()
        //初始化SwipeRefreshLayout
        initSwipeRefreshLayout()

    }

    //刷新
    private fun refresh() {
        // 这里的作用是防止下拉刷新的时候还可以上拉加载
        adapter.loadMoreModule.isEnableLoadMore = false
        // 下拉刷新，需要重置页数
        pageInfo.reset()
        loadMore()
    }

    //加载数据
    abstract fun loadMore()

    open fun loadMoreSuccess(data: List<T>) {
        swipeLayout.isRefreshing = false
        adapter.loadMoreModule.isEnableLoadMore = true


        if (pageInfo.isFirstPage()) {
            adapter.setList(data)
        } else {
            adapter.addData(data)
        }

        if (data.size < getPageSize()) {
            adapter.loadMoreModule.loadMoreEnd()
        } else {
            adapter.loadMoreModule.loadMoreComplete()
        }
        pageInfo.nextPage()
    }

    open fun loadMoreFailed() {
        swipeLayout.isRefreshing = false
        adapter.loadMoreModule.isEnableLoadMore = true
        adapter.loadMoreModule.loadMoreFail()
    }


    //子类中使用这个类来添加点击操作
    open fun initAdapter() {

        //设置开启动画
        adapter.animationEnable = true

        //分页加载
        adapter.loadMoreModule.setOnLoadMoreListener {
            loadMore()
        }

        //当自动加载开启，同时数据不满一屏时，是否继续执行自动加载更多(默认为true)
        //isAutoLoadMore是用来决定是否再在更多
        adapter.loadMoreModule.isAutoLoadMore = true
        adapter.loadMoreModule.isEnableLoadMoreIfNotFullPage = false

        //设置自定义View
        adapter.loadMoreModule.loadMoreView = CustomLoadMoreView()

    }

    open fun initRecycler() {
        recyclerView.adapter = adapter
        recyclerView.layoutManager = LinearLayoutManager(context)
    }

    open fun initSwipeRefreshLayout() {
        swipeLayout.setColorSchemeColors(Color.rgb(47, 223, 189))
        swipeLayout.setOnRefreshListener {
            refresh()
        }
    }

    //创建Adapter
    abstract fun createAdapter(): LoadMoreAdapter<T>

}